package com.youmingxing.adpter.selfInfo;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.youmingxing.R;
import com.youmingxing.bean.selfinfo.MyTicket;
import com.youmingxing.utils.TimeUtils;
import com.youmingxing.utils.ViewHolderUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的加油票
 */
public class MyTicketsAdapter extends BaseAdapter {
    private List<MyTicket> mList;
    private RelativeLayout relativeLayout;
    private TextView popularityValuesTv;
    private TextView popularityTime;
    private TextView popularitymethodTv;
    private View viewLine;
    private Context mContext;

    public MyTicketsAdapter(Context mContext) {
        mList = new ArrayList<>();
        this.mContext = mContext;
    }

    public void setData(List<MyTicket> list) {
        if (list == null)
            return;
        if (mList == null) {
            mList = list;
        } else {
            mList.clear();
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        if ((null != mList && mList.size() > 0) && (position >= 0 && position < mList.size())) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyTicket bean = mList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_my_tickets, null);
        }
        popularityValuesTv = ViewHolderUtils.getViewHolderView(convertView, R.id.my_tickets_useful);
        popularityTime = ViewHolderUtils.getViewHolderView(convertView, R.id.my_tickets_usetime);
        relativeLayout = ViewHolderUtils.getViewHolderView(convertView, R.id.myticket_itemrl);
        if (bean.valid) {
            relativeLayout.setBackgroundResource(R.mipmap.ticket_white);
            String time = TimeUtils.parseLeftSecondToString(bean.valid_second);
            popularityTime.setText(time);
            popularityValuesTv.setText("有效");

        } else {
            relativeLayout.setBackgroundResource(R.mipmap.ticket_grey);
            popularityTime.setText("已于" + bean.invalid_date + "过期");
            popularityValuesTv.setText("无效");

        }
        return convertView;
    }

}